﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    public class InputManager
    {
        MouseState currentMouseState;
        MouseState oldMouseState;
        KeyboardState currentKeyboardState;
        KeyboardState oldKeyboardState;
        Vector2 lastMousePressPos;
        //jagged arrays
        Keys[][] Combos = new Keys[][]{
            new Keys[] {Keys.Down , Keys.Right , Keys.X },
            new Keys[] {Keys.Down , Keys.Left , Keys.X },
            new Keys[] {Keys.Left , Keys.Left },
            new Keys[] {Keys.Left , Keys.Down , Keys.Right , Keys.X }
        };

        //Keys[,] Combos = {{Keys.Down , Keys.Right , Keys.X },
        //                 {Keys.Down , Keys.Left , Keys.X },
        //                 {Keys.Left , Keys.Left },
        //                 {Keys.Left , Keys.Down , Keys.Right , Keys.X }
        //                 };

        int MAXTIME;// = new int(500);
        int timeSinceLastKeyPress; // = new int(0);
        //Keys[] KeyBuffer;
        List<Keys> KeyBuffer = new List<Keys>();
        List<Keys> AcceptedKeys = new List<Keys>();

        public InputManager() {
            //KeyBuffer = new Keys[6];
            MAXTIME = 1000;
            AcceptedKeys.Add(Keys.Up);
            AcceptedKeys.Add(Keys.Down);
            AcceptedKeys.Add(Keys.Left);
            AcceptedKeys.Add(Keys.Right);
            AcceptedKeys.Add(Keys.Z);
            AcceptedKeys.Add(Keys.X);
        }

        public void Update(int dTime){
            oldMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();
            oldKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();
            if (isNewMouseClick())
            {
                lastMousePressPos = new Vector2(currentMouseState.X, currentMouseState.Y);
            }
            //if (isNewKeyPress()){
            //    KeyBuffer.Add(key);
            //}
            timeSinceLastKeyPress+=dTime;
            if (timeSinceLastKeyPress > MAXTIME)
                clearBuffer();

            foreach (Keys key in AcceptedKeys)
            {
                if (isNewKeyPress(key))
                {
                    KeyBuffer.Add(key);
                    timeSinceLastKeyPress = 0;
                }
            }
            //Keys[] tempbuffer = currentKeyboardState.GetPressedKeys();
            //if (tempbuffer.Length > 0)
            //{
            //}
        }

        public Vector2 getMousePos()
        {
            return new Vector2(currentMouseState.X, currentMouseState.Y);
        }

        public bool isNewMouseClick() {
            return currentMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released;
        }

        public bool isNewMouseRelease() {
            return currentMouseState.LeftButton == ButtonState.Released && oldMouseState.LeftButton == ButtonState.Released;
        }

        public bool isMouseDown() {
            return currentMouseState.LeftButton == ButtonState.Pressed;
        }

        public bool isNewKeyPress(Keys key) {
            return currentKeyboardState.IsKeyDown(key) && oldKeyboardState.IsKeyUp(key);
        }
        public bool isNewKeyRelease(Keys key){
            return currentKeyboardState.IsKeyUp(key) && oldKeyboardState.IsKeyUp(key);
        }

        public bool isKeyPressed(Keys key) {
            return currentKeyboardState.IsKeyDown(key);
        }

        void clearBuffer()
        {
            KeyBuffer.Clear();
        }

        public String getBuffer()
        {
            String temp = "";
            foreach (Keys key in KeyBuffer)
            {
                temp += " " + key;
            }
            return temp;
        }

        public int getEnteredCombo() {
            //int i;
            //bool found = true; 
            if (KeyBuffer.Count <= 0) return -1;
 //           if (KeyBuffer.Count < 3) return -1;
            for (int i = 0; i < Combos.GetLength(0); i++)
            {
                for (int j = 0; j < Combos[i].Length && j < KeyBuffer.Count; j++)
                {
                    if (Combos[i][j] != KeyBuffer[j])
                        break;

                    if (Combos[i].Length == KeyBuffer.Count && j == KeyBuffer.Count -1)
                        return i;
                }
            }
           // if (found) 
//            clearBuffer();
            return -1;
        }

        public bool wasComboEntered(int comboid)
        {

            return false;
        }
    }
}
